package cn.sunzq.springdata.elasticsearch.demo.config;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;


@Configuration
@MapperScan(basePackages =
        {"cn.sunzq.springdata.elasticsearch.demo.mapper.test"},
        sqlSessionFactoryRef = "testDataSqlSessionFactory")
public class TestDataDataSourceConfig {

    @Value("${spring.datasource.test.password:test1234}")
    private String password;

    @Value("${spring.datasource.test.jdbc-url:jdbc:mysql://10.122.149.207:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useAffectedRows=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai}")
    private String url;

    @Value("${spring.datasource.test.driver-class-name:com.mysql.cj.jdbc.Driver}")
    private String driverClassName;

    @Value("${spring.datasource.test.username:test_user}")
    private String username;

    @Autowired
    private HikariBaseConfig hikariBaseConfig;

    @Bean(name = "testDataDataSource")
    public DataSource testDataDataSource(){
        return hikariBaseConfig.getDataSource(driverClassName, url, username, password);
    }

    @Bean(name = "testDataSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("testDataDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/test/*.xml"));
        sessionFactoryBean.setPlugins(new Interceptor[]{pageHelper()});
        return sessionFactoryBean.getObject();
    }


    @Bean(name = "testDataSqlSessionProductTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("testDataSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws  Exception{
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "testDataTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("testDataDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean
    public JdbcTemplate jdbcTemplate(@Qualifier("testDataDataSource") DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public PageHelper pageHelper() {
        //分页插件
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        pageHelper.setProperties(properties);
        return pageHelper;
    }

}